/**
 * Baijiahulian.com Inc. Copyright (c) 2014-2014 All Rights Reserved.
 */
package com.baijia.dao.impl;

import com.baijia.dao.CommonDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.Serializable;

/**
 * @title CommonDaoImpl
 * @desc nothing to say
 * @author caoliang
 * @date 2015年12月5日
 * @version 1.0
 */
public abstract class CommonDaoImpl<T, PK extends Serializable> implements CommonDao<T, PK> {

    @Autowired
    @Qualifier("jdbcTemplate")
    protected JdbcTemplate jdbcTemplate;

    @Autowired
    @Qualifier("jdbcTemplateService")
    protected JdbcTemplate jdbcTemplateService;

    private Class<T> poClass;

    public CommonDaoImpl(Class<T> poClass) {
        this.poClass = poClass;
    }

    @Override
    public T getById(PK id) throws DataAccessException {
        return jdbcTemplate.queryForObject(selectSql() + " where id=?", new BeanPropertyRowMapper<>(poClass),
            new Object[] { id });
    }

    protected abstract String selectSql();
}
